import express from 'express';
import axios from 'axios';

const app = express();

app.use((req, res, next) => {
    res.header('Access-Control-Allow-Origin', '*');
    res.header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
    res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
    next();
});

app.get('/', (req, res) => {
    res.send('Hello from Proxy Server!');
});

app.use('/api', async (req, res) => {
    try {
        const url = 'https://news-at.zhihu.com' + req.url;
        const response = await axios({
            url,
            method: req.method,
            data: req.body,
            headers: req.headers
        });
        res.send(response.data);
    } catch (error) {
        res.status(error.response?.status || 500).send(error.message);
    }
});

app.listen(3000, () => {
    console.log('Proxy server is running on http://localhost:3000');
});
